<template>
  <div class="dialog-mask" v-if="visible" @click.self="close">
    <div class="dialog animated" :style="{ width, height }">
      <!-- 其他内容不变 -->
      <div class="dialog-header">
        <p class="dialog-header-title">{{ title }}</p>
      </div>
      <div class="dialog-content">
        <slot name="content">
          {{ content }}
        </slot>
      </div>
      <div class="dialog-footer">
        <slot name="footer">
          <div @click="close">取消</div>
          <div @click="handleOk">确定</div>
        </slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: '提示',
    },
    visible: {
      type: Boolean,
      default: false,
    },
    width: {
      type: String,
      default: '85%',
    },
    height: {
      type: String,
      default: '160px',
    },
    // ✅ 新增 content prop
    content: {
      type: String,
      default: '',
    },
    onConfirm: {
      type: Function,
      default: () => {},
    },
    onCancel: {
      type: Function,
      default: () => {},
    },
  },
  methods: {
    close() {
      this.fadeOut();
      this.onCancel();
    },
    handleOk() {
      this.fadeOut();
      this.onConfirm();
    },
    fadeOut() {
      const mask = this.$el; // 获取根元素
      mask.classList.add('closing'); // 触发退出动画

      // 等待动画结束再隐藏
      setTimeout(() => {
        this.visible = false;
        mask.classList.remove('closing');
      }, 200); // 与动画时间一致
    },
  },
};
</script>

<style lang="scss" scoped>
// 动画定义
@keyframes dialogScaleIn {
  0% {
    transform: translate(-50%, -50%) scale(0.5);
    opacity: 0;
  }
  70% {
    transform: translate(-50%, -50%) scale(1.1);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
}
@keyframes dialogScaleOut {
  0% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(0.5);
    opacity: 0;
  }
}
.dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 999;
  // 进入动画
  .dialog.animated {
    animation: dialogScaleIn 0.2s ease-out forwards;
  }

  // 离开动画（需要 JS 控制）
  &.closing .dialog.animated {
    animation: dialogScaleOut 0.2s ease-in forwards;
  }

  .dialog {
    background-color: #fff;
    border-radius: 16px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    // 位置
    position: absolute;
    top: 45%;
    left: 50%;
    transform: translate(-50%, -50%);
    .dialog-header {
      font-size: 20px;
      font-weight: bold;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      padding-top: 20px;
    }
    .dialog-content {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 16px;
      color: #ccc;
    }
    .dialog-footer {
      width: 100%;
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-top: 1px solid #ccc;
      color: #333;
      font-size: 16px;
      div {
        width: 50%;
        height: 100%;
        text-align: center;
        line-height: 50px;
        padding: 0 10px;
        -webkit-tap-highlight-color: transparent;
        cursor: pointer;
        &:nth-child(2) {
          color: #ef0924;
          border-left: 1px solid #ccc;
        }
      }
    }
  }
}
</style>
